//
//  main.swift
//  NB_swift_study
//  Created by zhanggang on 2018/7/6.
//  Copyright © 2018 zhang. All rights reserved.
//

import Foundation

var name = "Hello Swift 100!";

print("abc " + name)
print("def \(name)")

let number = 3; //常量
let `class` = "Run" //如果一定要使用关键字作为标识符，可以在关键字前后添加重音符号（`）

//number = 4; //会报错

let a = 1 + 3
//let b= 1 + 4  //Swift 空格

/*
Swift数据类型：Int UInt Float、Double Bool String Character Optional
    Int8	1 字节	-128 到 127
    UInt8	1 字节	0 到 255
    Int32	4 字节	-2147483648 到 2147483647
    UInt32	4 字节	0 到 4294967295
    Int64	8 字节	-9223372036854775808 到 9223372036854775807
    UInt64	8 字节	0 到 18446744073709551615
    Float	4 字节	1.2E-38 到 3.4E+38 (~6 digits)
    Double	8 字节	2.3E-308 到 1.7E+308 (~15 digits)
*/

var c:Int? = 33 //可选(Optionals)类型 当你声明一个可选变量或者可选属性的时候没有提供初始值，它的值会默认为 nil
var myString:String? = nil

if a > 4 {
    print("aaa")
}

if myString != nil , a > 3{
    print("aa")
}else{
    print("字符串为 nil")
}

for i in 1 ... 4{
    print(i)
}


//数组
var someInts = [Int](repeating: 10, count: 3) //第一种方式
var someVar = someInts[0]
print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

var someInts1 = [Int]()  //第二种方式
someInts1.insert(22, at: 0)


var someInts3:[Int] = [10, 20, 30]  ////第三种方式

print(someInts3.count,someInts3.isEmpty)


//字典
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someDictVar = someDict[1]
print( "key = 1 的值为 \(someDictVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )
someDict.updateValue("ttttt", forKey: 2)    //修改值
print( "key = 2 的值为 \(someDict[2])" )
for (key, value) in someDict {
    print("字典 key \(key) -  字典 value \(value)")
}
for value in someDict.values {
    print(value)
}

//函数
func say(name:String) -> String{
    let str = "hello 函数测试 " + name

    var demo = "demo"
    demo.append(" tt")
    demo.append(" tt")

    return demo
}
print(say(name: "Test"))

//闭包
let divideOper = {
    (var1:Int,var2:Int) -> Int in
    return var1 / var2
}
var result = divideOper(15,3)
print("闭包",result)

//枚举
enum Student{
    case Name(String)
    case Mark(Int,Int,Int)
}
var studDetails = Student.Name("Runoob")
var studMarks = Student.Mark(98,97,95)
switch studMarks {
case .Name(let studName):
    print("学生的名字是: \(studName)。")
case .Mark(let Mark1, let Mark2, let Mark3):
    print("学生的成绩是: \(Mark1),\(Mark2),\(Mark3)。")
}

//结构体
struct studentMarks {
    var mark1 = 100
    var mark2 = 78
    var mark3 = 98
}
let marks = studentMarks()
print("Mark1 是 \(marks.mark1)")
print("Mark2 是 \(marks.mark2)")
print("Mark3 是 \(marks.mark3)")
struct MarksStruct {
    var mark: Int

    init(mark: Int) {
        self.mark = mark
    }
}
var aStruct = MarksStruct(mark: 98)
print(aStruct.mark) // 98

//类
var student = Student1.init(name: "dsczs", age: 333)
student.toString()

//泛型
func swap( a: inout Int, b: inout Int){ //正常方法
    let tmp = a
    a = b
    b = tmp
}
func swap2<T>( a: inout T, b: inout T){ //泛型方法
    let tmp = a
    a = b
    b = tmp
}
var numb1 = 33
var numb2 = 55
swap(a: &numb1, b: &numb2)
print("swap",numb1,numb2)
swap2(a: &numb1, b: &numb2)
print("swap2",numb1,numb2)


print(name);

print(number);
